var Workport = Workport || {};
// @Path("/Alert")
Workport.CommunityAPI = (function(){

	var root = WP_RESTFUL_URL + (WP_RESTFUL_URL.endsWith('/') ? '' : '/') + 'Community';
	
	var urls = ["", "/{1}", "/{1}/Membership", "/Membership", "/Subscribe", "/Search", "/{1}/Expert", "/Nominate", "/CommunityStats", "/New",
	            "/{1}/Exist", "/Types", "/{1}/Edit", "{1}/Export" ];
	
	function getUrl(/*index, replace1, replace2, ...*/) {
		var index, i, loops, r;
		loops = arguments.length;
		if (loops > 0) {
			index = arguments[0];
			r = root + urls[index];
			for (i = 1; i < loops; i++){
				r = r.replace("{"+i+"}", arguments[i]);
			}
		}
		return r;
	}
	
	function sendRequest(method,url,callback,query,sync,data){
		if(method == 'get'){
			if(sync){
				return sfw.Http.syncGet(url,callback,query);
			} else {
				return sfw.Http.get(url,callback,query);
			}
		} else if(method == 'post'){
			if(sync){
				return sfw.Http.syncPost(url,callback,query,data);
			} else {
				return sfw.Http.post(url,callback,query,data);
			}
		} else if(method == 'put'){
			if(sync){
				return sfw.Http.syncPut(url,callback,query,data);
			} else {
				return sfw.Http.put(url,callback,query,data);
			}
		} 
	}
	
	return {
		// @GET		@Path("/")
		// public CommunityResponse GetCommunity()
		getCommunityHome : function(handler,request){
			return sendRequest('get',getUrl(0),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				pn:request.pagePosition ? request.pagePosition : 1,
				ps:request.pageSize ? request.pageSize : 10,
				Key:request.key ? request.key : "" ,
				CommunityType:request.type
			},request.sync);
			function toViewModel(data){
				var view = {};
				view.code = data.ResponseCode;
				view.message = data.ResponseMessage;
				view.data = [];
                var index = 0;
				if(data.CommunityItems){
					for(var i=0;i<data.CommunityItems.length;i++){
                        if(data.CommunityItems[i] != null) {
                            index = view.data.length;
                            view.data[index] = {};
                            view.data[index].CommunityID = data.CommunityItems[i].CommunityID;
                            view.data[index].CommunityName = data.CommunityItems[i].CommunityName;
                            view.data[index].imgUrl = data.CommunityItems[i].CommunityLogoPathUrl;
                            view.data[index].CommunityStatus = data.CommunityItems[i].CommunityStatus;
                            view.data[index].CommunityType = data.CommunityItems[i].CommunityType;
                            view.data[index].membersCount = data.CommunityItems[i].CommunityMembersCount;
                            //view.data[index].count = data.CommunityItems[i].CommunityMembersCount;
                        }
					}
				}
				return view;
			}
		},
		// @GET		@Path("/{communityId}")
		// public CommunityDetailResponse GetCommunityProfileDetails()
		getCommunityDetail:function(handler,request){
			return sendRequest('get',getUrl(1,request.communityId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},null,request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/{communityId}/Membership")
		// public CommunityMembershipResponse GetCommunityUserDetails()
		getCommunityMemberShip:function(handler,request){
			return sendRequest('get',getUrl(2,request.communityId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				ps : request.pageSize,
				sinceid : request.pagePosition,
				membertype : request.type
			},request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @POST		@Path("/Membership")
		// public BasicResponse PostJoinLeaveStatus(MembershipRequest request)
		postMemberShip:function(handler,request){
			return sendRequest('post',getUrl(3),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				communityId : request.communityId,
				actionType : request.action
			},request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @PUT		@Path("/Membership")
		// public BasicResponse PutApproveRejectRequest(PutMembershipRequest request)
		putMemberShip:function(handler,request){
			return sendRequest('put',getUrl(3),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				communityId : request.communityId,
				pendingUser : request.user,
				ActionStatus : request.action,
				notificationId : request.notificationId 
			},request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @POST		@Path("/Subscribe")
		// public BasicResponse PostCommunitySubscribe(PostSubscribeRequest request)
		postSubscribe:function(handler,request){
			return sendRequest('post',getUrl(4),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				CommunityId : request.communityId,
				ActionType : request.action
			},request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/Search")
		// public CommunitySearchResponse GetSearchedCommunity()
		search:function(handler,request){
			return sendRequest('get',getUrl(5),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				pn : request.pagePosition,
				ps : request.pageSize,
				Key : request.key,
				CommunityType : request.type
			},request.sync);
			function toViewModel(data){
                var view = {};
                view.code = data.ResponseCode;
                view.message = data.ResponseMessage;
                view.data = [];
                var index = 0;
                if(data.CommunityItems){
                    for(var i=0;i<data.CommunityItems.length;i++){
                        if(data.CommunityItems[i] != null) {
                            index = view.data.length;
                            view.data[index] = {};
                            view.data[index].CommunityID = data.CommunityItems[i].CommunityID;
                            view.data[index].CommunityName = data.CommunityItems[i].CommunityName;
                            view.data[index].imgUrl = data.CommunityItems[i].CommunityLogoPathUrl;
                            view.data[index].CommunityStatus = data.CommunityItems[i].CommunityStatus;
                            view.data[index].CommunityType = data.CommunityItems[i].CommunityType;
                            view.data[index].membersCount = data.CommunityItems[i].CommunityMembersCount;
                            //view.data[index].count = data.CommunityItems[i].CommunityMembersCount;
                        }
                    }
                }
                return view;
			}
		},
		// @GET		@Path("/{notificationId}/Expert")
		// public ExportResponse GetExpertDetails(@PathParam("notificationId") String notificationId)
		getExport:function(handler,request){
			return sendRequest('get',getUrl(6,request.notificationId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				rowno : request.pagePosition,
				ps : request.pageSize,
				NotificationType : request.notificationType,
				TargetId : request.targetId
			},request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @POST		@Path("/Nominate")
		// public BasicResponse UpdateExpertDetails(NominateRequest request)
		nominate:function(handler,request){
			return sendRequest('post',getUrl(7),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				NotificationId : request.notificationId,
				Cid : request.targetId,
				ExpertsIds : request.exports,
				Tags : request.tags
			},request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/CommunityStats")
		// public CommunityStatsResponse GetCommunityStats()
		getStat:function(handler,request){
			return sendRequest('get',getUrl(8),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				Cid : request.communityId
			},request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @POST		@Path("/New")
		// public CommunityCreatResponse PostCommunity(CommunityCreatRequest request)
		create:function(handler,request){
			var domain = {
					CommunityName : request.name,
					CommunityDescription : request.description,
					StartDate : request.start,
					EndDate : request.end,
					CommunityNickName : request.nickname,
					CommunityType : request.type,
					CommunityLogo : request.logo,
					CommunityTags : request.tags,
					CommunityApps : request.apps,
					Members : request.memebers,
					Moderators : request.managers
				};
			return sendRequest('post',getUrl(9),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},domain,request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/{Name}/Exist")
		// public ExistResponse GetNameAvailability(@PathParam("Name") String name)
		exist:function(handler,request){
			return sendRequest('get',getUrl(10,request.name),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				type : request.type
			},request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/Types")
		// public CommunityTypeResponse GetCommunityTypes()
		getTypes:function(handler,request){
			return sendRequest('get',getUrl(11),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},null,request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @PUT		@Path("/{communityId}/Membership")
		// public BasicResponse PutAddOrRemoveUsers(@PathParam("communityId") String communityId,List<MemberRequest> request)
		postCommunityMemberShip:function(handler,request){
			var domain = [];
			for(var i=0;i<request.users.length;i++){
				domain[i] = {};
				domain[i].UserID = request.users[i].user;
				domain[i].Action = request.users[i].action;
				domain[i].IsModerator = request.users[i].isManager;
			}
			return sendRequest('put',getUrl(2,request.communityId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},domain,request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @PUT		@Path("/{communityId}/Edit")
		// public BasicResponse PutEditCommunity(@PathParam("communityId") String communityId,CommunityEditRequest request)
		editCommunity:function(handler,request){
			var domain = {};
			domain.CommunityStatus = request.status;
			domain.CommunityName = request.name;
			domain.CommunityDescription = request.description;
			domain.StartDate = request.startDate;
			domain.EndDate = request.endDate;
			domain.CommunityNickName = request.nickname;
			domain.CommunityType = request.type;
			domain.CommunityLogo = request.logo;
			domain.CommunityTags = request.tags;
			domain.CommunityApps = request.apps;
			domain.CommmunityMembers = [];
			for(var i=0;i<request.members.length;i++){
				domain.CommmunityMembers[i] = {};
				domain.CommmunityMembers[i].UserID = request.members[i].user;
				domain.CommmunityMembers[i].Action = request.members[i].action;
				domain.CommmunityMembers[i].IsModerator = request.members[i].isManager;
			}
			domain.CommmunityModerators = [];
			for(var i=0;i<request.managers.length;i++){
				domain.CommmunityModerators[i] = {};
				domain.CommmunityModerators[i].UserID = request.managers[i].user;
				domain.CommmunityModerators[i].Action = request.managers[i].action;
				domain.CommmunityModerators[i].IsModerator = request.managers[i].isManager;
			}
			return sendRequest('put',getUrl(12,request.communityId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},domain,request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/Types")
		// public CommunityTypeResponse GetCommunityTypes()
		getExports:function(handler,request){
			return sendRequest('get',getUrl(13,request.notificationId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				NotificationType		: 	request.notificationType,
				TargetId				:	request.targetId,
				ps						: 	request.pageSize,
				rowno					:	request.pagePosition
			},request.sync);
			function toViewModel(data){
				return data;
			}
		}
	}
})();
